package com.example.second.dao;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.PreparedStatementCreator;
import org.springframework.jdbc.support.GeneratedKeyHolder;
import org.springframework.jdbc.support.KeyHolder;
import org.springframework.stereotype.Repository;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Repository
public class StorageDaoImpl implements IStorageDao{

    @Autowired
    JdbcTemplate jdbcTemplate;

    public Map<String,Object> insertStorage(String sku_id, double in_quanty, double out_quanty){
        int new_id = 0;
        final double quanty = in_quanty-out_quanty;
        boolean result = false;
        Map<String,Object> resultMap = new HashMap<String, Object>();

        String sql = "SELECT id FROM tb_stock_storage WHERE sku_id = ?";
        ArrayList<Map<String,Object>> list = (ArrayList<Map<String,Object>> )
                jdbcTemplate.queryForList(sql,sku_id);
        if(!list.isEmpty()&&list.size()>0){
            new_id = Integer.parseInt(list.get(0).get("id").toString());
        }
        else {
            KeyHolder keyHolder = new GeneratedKeyHolder();
            final String finalsql = "INSERT INTO tb_stock_storage(warehouse_id,sku_id,quanty)"+
                    " VALUES(1,"+sku_id+","+quanty+")";

            result = jdbcTemplate.update(new PreparedStatementCreator() {
                public PreparedStatement createPreparedStatement(Connection connection) throws SQLException {
                    return connection.prepareStatement(finalsql, Statement.RETURN_GENERATED_KEYS);
                }
            }, keyHolder)==1;
            if(!result){
                resultMap.put("result", false);
                resultMap.put("msg", "主表不存在商品库存信息，写入主表失败");
                return resultMap;
            }
            new_id = keyHolder.getKey().intValue();
        }

        //写入历史表
        String sql2 = "INSERT INTO tb_stock_storage_history(stock_storage_id,in_quanty,out_quanty) VALUES(?,?,?)";
        result = jdbcTemplate.update(sql2, new_id,in_quanty,out_quanty)==1;
        if(!result){
            resultMap.put("result", false);
            resultMap.put("msg", "写入历史表失败");
            return resultMap;
        }
        //更新主表
        if(!list.isEmpty()&&list.size()>0){
            String sql3 = "UPDATE tb_stock_storage SET quanty = quanty + "+ quanty +" WHERE id = "+new_id ;
            result = jdbcTemplate.update(sql3)==1;
            if(!result){
                resultMap.put("result", false);
                resultMap.put("msg", "主表存在库存信息，更新失败");
                return resultMap;
            }
        }

        resultMap.put("result", true);
        resultMap.put("msg", "成功更新库存");
        return resultMap;
    }

    public boolean updateOrderStatus(String order_id){
        String sql1 = "SELECT order_id FROM tb_order WHERE order_id = ?";
        int i = 0;
        while (i++<5){
            try {
                Thread.sleep(10);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            List<Map<String, Object>> empty = jdbcTemplate.queryForList(sql1, order_id);
            if (empty!=null&&!empty.isEmpty()){
                break;
            }
        }
        String sql2 = "update tb_order set status = 2 where order_id = ?";
        return jdbcTemplate.update(sql2, order_id)==1;
    }
}
